import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:happy_study/ui/shared/app_theme.dart';

import 'main_logic.dart';

class Mainview extends StatelessWidget {
  final logic = Get.put(MainLogic());
  final state = Get.find<MainLogic>().state;
  static String routeName = "/main";

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MainLogic>(builder: (logic) {
      return Scaffold(
        body: SingleChildScrollView(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                decoration: BoxDecoration(
                    color: HYAppTheme.norGray01Color,
                    borderRadius: BorderRadius.only(
                        bottomLeft: Radius.circular(5.r),
                        bottomRight: Radius.circular(5.r))),
                alignment: Alignment.center,
                width: .6.sw,
                height: 30.h,
                child: Text("讲台"),
              ),
              5.verticalSpace,
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  buildWindows(),
                  Container(
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(10.r)),
                      color: HYAppTheme.norGray01Color,
                    ),
                    padding: EdgeInsets.all(10.r),
                    width: .9.sw,
                    child: GridView.builder(
                      shrinkWrap: true,
                      physics: const NeverScrollableScrollPhysics(),
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 4,
                          childAspectRatio: 2,
                          mainAxisSpacing: 20.r,
                          crossAxisSpacing: 30.r),
                      itemBuilder: (ctx, index) {
                        return buildStudentGroup(index);
                      },
                      itemCount: state.studentGroups.length,
                    ),
                  ),
                  buildWindows()
                ],
              ),
              30.verticalSpace,
            ],
          ),
        ),
      );
    });
  }

  Widget buildWindows() {
    return Container(
      color: HYAppTheme.norGray01Color,
      height: 1.sh,
      child: Text("窗"),
    );
  }

  Widget buildStudentGroup(index) {
    return GridView.builder(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: state.groupNum,
        crossAxisSpacing: 2.r,
      ),
      itemBuilder: (context, i) {
        return buildStudent(state.studentGroups[index][i]);
      },
      itemCount: state.studentGroups[index].length,
    );
  }

  Widget buildStudent(student) {
    return GestureDetector(
      onTap: () {
        logic.showStudentDetailDialog(student);
      },
      child: Container(
        alignment: Alignment.center,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(4.r)),
            color: Colors.blue),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              student.studentName,
              style: TextStyle(fontSize: 12.sp),
            ),
            Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text("${student.studentLevel}",
                    style: TextStyle(fontSize: 10.sp)),
                Container(
                  padding: EdgeInsets.all(5.r),
                  width: 15.r,
                  height: 15.r,
                  child: CircularProgressIndicator(
                    color: Colors.yellow,
                    value: (student.studentExperience / 100).toDouble(),
                  ),
                ),
              ],
            ),
            3.verticalSpace,
            Text("${student.studentExperience} / 100",
                style: TextStyle(fontSize: 10.sp)),
          ],
        ),
      ),
    );
  }
}
